var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
$CertusFormAjax.ajax('initDepts',{},function (response) {
    var data = response.data;
    $$scope.depts = data;
    if (data.length==1){
        $("#deptCode").attr("disabled","disabled");
    }
    $$scope.deptCode = data[0].departmentCode;
});

$CertusFormAjax.ajax('findAllVtType',{},function (response) {
    $$scope.type = response.data;
});
$CertusFormAjax.ajax("findVtlists",{"processId":processId}, function(result){
    var data = result.data;
    $$scope.vacationType = data.vacationType+"";
    $$scope.beginTime = data.beginTime;
    $$scope.start = data.start;
    $$scope.endTime = data.endTime;
    $$scope.end = data.end;
    $$scope.vacationDay = data.vacationDay;
    $$scope.vacationHour = data.vacationHour+"";
    $$scope.totalHour = data.totalHour;
    $$scope.telephone = data.telephone;
    $$scope.comments = data.comments;
    $$scope.opinionMsg = data.opinionMsg;
    $$scope.deptCode = data.deptCode;
    $$scope.id = data.id;
});

this.calcHours = function (days) {
    var vacationHour = $("#vacationHour").val();
    if (days==""){
        return;
    }
    var totalHours = Number(days*8) + Number(vacationHour);
    $("#totalHour").val(totalHours);
};

this.calcueDay = function (hours) {
    var vacationDay = $("#vacationDay").val();
    if (vacationDay==""){
        $("#totalHour").val("");
        return;
    }
    var totalHours = Number(vacationDay*8) + Number(hours);
    $("#totalHour").val(totalHours);
};

//系统计算两日期相差多少个小时
this.getInervalHour=function(startDate, endDate) {
    var d1 = new Date(startDate);
    var d2 = new Date(endDate);
    return parseInt(d2 - d1) / 1000 / 3600+24;
};

$(".submit").on('click',function () {

    // 获取开始日期时间
    var telephone = $("#telephone").val();
    var comments = $("#comments").val();
    var vacationDay = $("#vacationDay").val();
    var vacationHour = $("#vacationHour").val();

    // 获取开始日期时间
    var startDay = $("#beginTime").val();
    var startTime = $("#start option:selected").val();
    var startDT = startDay + " " + startTime;
    // console.log("结束日期时间：" + endDT);

    // 获取结束日期时间
    var endDay = $("#endTime").val();
    var endTime = $("#end option:selected").val();
    var endDT = endDay + " " + endTime;

    var diffTHours = getInervalHour(startDT,endDT);
    var diffDay = parseInt(diffTHours/24);
    // var diffChours = diffTHours%24;


    // if (diffDay!=vacationDay){
    //     layer.msg("请假总时长填写不正确，请重新填写！正确日期为："
    //         +diffDay+"天");
    //     return;
    // }
    if (telephone == "" || telephone == null || telephone == undefined) {
        layer.msg("紧急联系方式不能为空！");
        return;
    }
    if (comments == "" || comments == null || comments == undefined) {
        layer.msg("请假原因不能为空！");
        return;
    }

    // 转换开始标准时间
    var start = new Date(startDT);
    // console.log("开始标准时间：" + start);
    // 转换结束标准时间
    var end = new Date(endDT);
    // console.log("结束标准时间：" + end);
    // 当前日期及时间
    var toDay = new Date();
    // console.log("当前标准时间：" + toDay);

    // if (Date.parse(start) < Date.parse(toDay)) {
    //     layer.msg("开始时间不能小于当前时间！");
    //     return;
    // }
    // if (Date.parse(end) < Date.parse(toDay)) {
    //     layer.msg("结束时间不能小于当前时间！");
    //     return;
    // }
    if (Date.parse(end) == Date.parse(start)) {
        layer.msg("结束时间不能等于开始时间！");
        return;
    }
    if (Date.parse(end) < Date.parse(start)) {
        layer.msg("结束时间不能小于开始时间！");
        return;
    }

    // 截取时间保留分钟
    var starMinutes = Number(startTime.substring(3, 5));
    var endMinutes = Number(endTime.substring(3, 5));
    if (starMinutes != endMinutes) {
        layer.msg("请假最小时长计量单位：一小时");
        return;
    }
    if ($("#totalHour").val() == "0") {
        layer.msg("请假总时长不能为0");
        return;
    }

    var arr = $("form").serializeArray();
    var data = {};
    for (var i = 0; i < arr.length; i++) {
        data[arr[i].name] = arr[i].value;
    };
    data.id = $$scope.id;
    data.totalHour = $("#totalHour").val();
    data.deptCode = $("#deptCode").val();
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('editLeave',{"param":JSON.stringify(data),"stepId":stepId,"processId":processId},function () {
        //关闭
        layer.close(index);
        layer.msg("申请成功");
        window.history.back()
    });
});